﻿appRoot.controller('RemoteController', function ($scope, $location, $resource) {

    $scope.selectedServices = [];

    $scope.$watchCollection('selectedServices', function () {
        $scope.selectedService = angular.copy($scope.selectedServices[0]);
    });

    $scope.serviceGrid = {
        data: 'serviceList',
        multiSelect: false,
        selectedItems: $scope.selectedServices,
        enableColumnResize: false,
        columnDefs: [
            {
                field: 'name', displayName: 'Service Name', width: '200px', cellTemplate:
                  '<div class="ngCellText"><i ng-class="{\'icon-cube fg-green on-left-more\': row.getProperty(\'status\')==0, \'icon-cube fg-red on-left-more\': row.getProperty(\'status\')==1}"></i>{{row.getProperty(col.field)}}</div>'
            },
            { field: 'description', displayName: 'Description' },
            {
                field: '', displayName: 'Status', cellTemplate:
                  '<div class="ngCellText"><div ng-class="{\'fg-green\': row.getProperty(\'status\')==0, \'fg-red\': row.getProperty(\'status\')==1}">'
                  + '<div ng-switch on="row.getProperty(\'status\')">'
                    + '<div class="text-bold" ng-switch-when=0>Running</div>'
                    + '<div class="text-bold" ng-switch-when=1>Stopped</div>'
                    + '<div ng-switch-default >Unknown</div>'
                  + '</div>'
                  + '</div></div>'
            }
            // { field: '', cellTemplate: '<button class="btn btn-mini btn-danger" ng-click="">Delete</button>' }
        ]
    };

    $scope.getServices = function () {

        var serviceResource = $resource('/api/services', {}, { update: { method: 'PUT' } });

        $scope.serviceList = [];

        serviceResource.query(function (data) {
            $scope.serviceList = [];
            angular.forEach(data, function (serviceData) {
                $scope.serviceList.push(serviceData);
            });
        });
    };
});